package com.ruoyi.project.rf.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.project.bas.msgTray.domain.TrayCodeMsg;

import com.ruoyi.project.catType.mapper.WmsCallTcarMapper;
import com.ruoyi.project.in.stockin.mapper.StockinMapper;
import com.ruoyi.project.in.stockindetail.domain.StockinDetail;
import com.ruoyi.project.in.stockindetail.mapper.StockinDetailMapper;
import com.ruoyi.project.mydo.detail.domain.MydoJobDetail;
import com.ruoyi.project.mydo.detail.mapper.MydoJobDetailMapper;
import com.ruoyi.project.mydo.mydojob.domain.MydoJob;
import com.ruoyi.project.mydo.mydojob.service.IMydoJobService;
import com.ruoyi.project.mydo.mydojob.mapper.MydoJobMapper;
import com.ruoyi.project.pandian.commit.domain.PdCommitRecord;
import com.ruoyi.project.pandian.commit.mapper.PdCommitRecordMapper;
import com.ruoyi.project.pandian.record.mapper.PdRecordMapper;
import com.ruoyi.project.rf.domain.BasJxTaiChe;
import com.ruoyi.project.rf.mapper.rfMapper;
import com.ruoyi.project.rf.service.IPdaService;
import com.ruoyi.project.stock.stock.domain.BasStock;
import com.ruoyi.project.stock.stock.mapper.BasStockMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 物资入库Service业务层处理
 *
 * @author wz
 * @date 2021-11-16
 */
@Service
public class PdaServiceImpl implements IPdaService {

    @Autowired(required = false)
    private EntityManager entityManager;

    @Autowired
    private IPdaService pdaService;
    @Autowired
    private BasStockMapper basStockMapper;

    private StockinMapper stockinMapper;

    @Autowired
    private StockinDetailMapper stockinDetailMapper;

    @Autowired
    private rfMapper rfMapper;
    @Autowired
    private PdCommitRecordMapper commitRecordMapper;
    @Autowired
    private MydoJobDetailMapper mydoJobDetailMapper;
    @Autowired
    private IMydoJobService mydoJobService;

    @Autowired
    private MydoJobMapper mydoJobMapper;

    @Autowired
    private PdRecordMapper pdRecordMapper;

    @Autowired
    private WmsCallTcarMapper wmsCallTcarMapper;


    @Override
    public Map<String, String> msgSure(TrayCodeMsg trayCodeMsg) {
        System.out.println(JSONObject.toJSON(trayCodeMsg));
        Map<String, String> map = new HashMap();

//
        //0、该托盘的作业是否未执行
        MydoJob mydoJob = new MydoJob();
        mydoJob.setTrayCode(trayCodeMsg.getTrayCode());
        List<MydoJob> mydoJobs = mydoJobService.selectMydoJobList(mydoJob);
        if (mydoJobs.size() > 0 && !"0".equals(mydoJobs.get(0).getStatus())) {
            map.put("v_resutmsg", "该托盘已有作业！");
            System.out.println(JSONObject.toJSON(map));
            return map;
        }
        //查询是否已经绑定该物料信息
        MydoJobDetail mydoJobDetail = new MydoJobDetail();
        mydoJobDetail.setInOrOutDId(trayCodeMsg.getStockinDetailId());
        mydoJobDetail.setProductId(trayCodeMsg.getProductId());
        mydoJobDetail.setLotNo(trayCodeMsg.getLotNo());
        List<MydoJobDetail> mydoJobDetails = mydoJobDetailMapper.selectMydoJobDetailList(mydoJobDetail);
        if (mydoJobDetails.size() > 0) {
            map.put("v_resutmsg", "相同物料重复添加！");
            System.out.println(JSONObject.toJSON(map));
            return map;
        }
        //1、验证数量
//        StockinDetail stockinDetail = pdaService.selectstockinDetail(trayCodeMsg);
        int quantity = trayCodeMsg.getQuantity() - trayCodeMsg.getQuantityX();
        if (quantity < trayCodeMsg.getInQuantity()) {
            map.put("v_resutmsg", "实际填入数量大于计划数量");
            System.out.println(JSONObject.toJSON(map));
            return map;
        }
//        //2、验证托盘已存在物资是否为同一垛型
//        String stackType = pdaService.selectStackTypeByTrayCode(trayCodeMsg.getTrayCode());
//        if (stackType != null && !trayCodeMsg.getStackingId().equals(stackType)) {
//            map.put("v_resutmsg", "同一托盘需统一垛型！");
//            System.out.println(JSONObject.toJSON(map));
//            return map;
//        }
//        //3、计算层数
//        if ("3".equals(trayCodeMsg.getStackingId())) {
//            trayCodeMsg.setStackingId("4");
//        }
//        double ceil = Math.ceil(Double.valueOf(trayCodeMsg.getQuantity()) / Double.valueOf(trayCodeMsg.getStackingId()));
//
//

        map.put("v_stockin_detail_id", trayCodeMsg.getStockinDetailId());
        map.put("v_stockin_id", trayCodeMsg.getStockinId());
        map.put("v_tray_code", trayCodeMsg.getTrayCode());
        map.put("v_lot_no", trayCodeMsg.getLotNo());
        map.put("v_in_quantity", String.valueOf(trayCodeMsg.getInQuantity()));
        map.put("v_product_name", trayCodeMsg.getProductName());
        map.put("v_product_id", trayCodeMsg.getProductId());
        map.put("v_product_status", trayCodeMsg.getStockStatus());
        map.put("v_assign_num", trayCodeMsg.getAssignNum());
        map.put("v_print_date", trayCodeMsg.getPrintDate());
        map.put("v_supplier_id", trayCodeMsg.getSupplierId());
        map.put("v_spec", trayCodeMsg.getSpec());
        map.put("v_type", trayCodeMsg.getType());


        map.put("v_result", null);
        map.put("v_resutmsg", null);
//
        rfMapper.trayCodeAdd(map);
//        System.out.println(JSONObject.toJSON(map));
        return map;
    }


    @Override
    public Map<String, String> refluxSure(String trayCode) {
        Map<String, String> map = new HashMap();
        map.put("v_tray_code", trayCode);


        map.put("v_result", null);
        map.put("v_resutmsg", null);

        rfMapper.refluxAdd(map);
        System.out.println(JSONObject.toJSON(map));
        return map;
    }


    @Override
    public Map<String, String> selectFinishOut(String trayCode) {
        Map<String, String> map = new HashMap();
        map.put("v_tray_code", trayCode);


        map.put("v_result", null);
        map.put("v_resutmsg", null);

        rfMapper.refluxAdd(map);
        System.out.println(JSONObject.toJSON(map));
        return map;
    }

    @Override
    public List<String> selectInStockIdByStatus(String status, String inType) {
        return stockinMapper.selectInStockIdByStatus(status, inType);
    }

    public List<String> selectInStockDIdByStockId(String stockId) {
        return stockinDetailMapper.selectInStockdIdByStockId(stockId);
    }

    @Override
    public TrayCodeMsg selectstockinDetailById(String inStockDetailId) {

        return stockinDetailMapper.selectStockinDetail(inStockDetailId);
    }

    @Override
    public List<StockinDetail> selectstockinDetail(TrayCodeMsg trayCodeMsg) {
        StockinDetail stockinDetail = new StockinDetail();
//        stockinDetail.setProductId(trayCodeMsg.getProductId());
//        stockinDetail.setPrintDate(trayCodeMsg.getPrintDate());
//        stockinDetail.setSupplierId(trayCodeMsg.getSupplierId());

        return stockinDetailMapper.selectStockinDetailList(stockinDetail);
    }


    @Override
    public String selectStackTypeByTrayCode(String trayCode) {


        return stockinDetailMapper.selectStackTypeByTrayCode(trayCode);
    }


    @Override
    public Map<String, String> saveJob(MydoJobDetail mydoJobDetail) {

        Map<String, String> map = new HashMap();
        map.put("v_product_id", mydoJobDetail.getProductId());
        map.put("v_length", mydoJobDetail.getLength());
        map.put("v_grade", mydoJobDetail.getGrade());
        map.put("v_entranceId", mydoJobDetail.getExitId());
        map.put("v_in_or_out", "1");

        map.put("v_result", null);
        map.put("v_resutmsg", null);

        rfMapper.productJobAdd(map);


        return map;
    }

    @Override
    public AjaxResult saveJobX(StockinDetail stockinDetail) {

        Map<String, Object> map = new HashMap();

        map.put("v_stockin_detail_id", stockinDetail.getStockinDetailId());
        map.put("v_tray_code", stockinDetail.getTrayCode());
        map.put("v_product_name", stockinDetail.getProductName());
        map.put("v_lot_no", stockinDetail.getLotNo());
        map.put("v_quantity", stockinDetail.getQuantity());
        map.put("v_warehouse_id", stockinDetail.getWarehouseId());
        map.put("v_result", null);
        map.put("v_resutmsg", null);

        rfMapper.productJobAddX(map);
        int result = (int) map.get("v_result");

        if (result == 10) {
            return AjaxResult.success(map.get("v_resutmsg").toString());
        } else {
            return AjaxResult.error(map.get("v_resutmsg").toString());
        }


    }

    @Override
    public String outStockSure(String trayCode) {

        Integer inOrOut = mydoJobMapper.selectInOutByCode(trayCode);
        Map<String, Object> params = new HashMap();
        String msg = "";
        int resultCode = 0;
        params.put("trayCode", trayCode);
        if (inOrOut == 10 || inOrOut == 12 || inOrOut == 16) {
            rfMapper.checkStockSure(params);
            resultCode = (int) params.get("resultCode");
            if (resultCode == 1) {
                msg = "无该条码任务，请检查！";
            } else if (resultCode == 10) {
                msg = "操作成功！";
            } else {
                msg = "未知的返回值，请联系管理员！";
            }
        } else if (inOrOut == 11 || inOrOut == 13) {
            rfMapper.outStockSure(params);
            resultCode = (int) params.get("resultCode");
            if (resultCode == 1) {
                msg = "无该条码任务，请检查！";
            } else if (resultCode == 10) {
                msg = "操作成功！";
            } else {
                msg = "未知的返回值，请联系管理员！";
            }
        }

        return msg;
    }

    @Override
    public AjaxResult outStockSureL(String trayCode) {
        String msg = "";
        Map<String, Object> params = new HashMap();
        params.put("trayCode", trayCode);
        params.put("warehouseId", "02");
        params.put("result", 0);
        params.put("result_msg", "");


        rfMapper.outSure(params);
        int result = (int) params.get("result");
        msg = (String) params.get("result_msg");
        if (result == 10) {
            return AjaxResult.success(msg);
        } else {
            return AjaxResult.error(msg);
        }

    }

    @Override
    public List<MydoJobDetail> checkMsg(String trayCode) {
        return mydoJobDetailMapper.checkMsg(trayCode);
    }

//    @Override
//    public String pdStockSure(List<MydoJobDetail> detailList) {
//        try {
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            for (MydoJobDetail detail : detailList) {
//                // 根据作业子表ID查出子表信息
//                MydoJobDetail mydoJobDetail = mydoJobDetailMapper.selectMydoJobDetailByJobDetailId(detail.getJobDetailId());
//                if (mydoJobDetail != null) {
//                    //修改后的数量与记录数量相等，不做任何操作
//                    if (detail.getInQuantity() == mydoJobDetail.getInQuantity()) {
//                        continue;
//                    }
//
//                    //记录不相等，做更新操作
//                    if (detail.getOutQuantity().intValue() != 0 && detail.getOutQuantity() != mydoJobDetail.getOutQuantity()) {
//                        //盘点记录更新
//                        PdRecord pdRecord = new PdRecord();
//                        pdRecord.setPdGuid(UUID.randomUUID().toString());
//                        pdRecord.setStockId(mydoJobDetail.getStockId());
//                        pdRecord.setTrayCode(detail.getTrayCode());
//                        pdRecord.setProductId(mydoJobDetail.getProductId());
//                        pdRecord.setSupplier(mydoJobDetail.getSpec());
//                        pdRecord.setOldQuantity(String.valueOf(mydoJobDetail.getInQuantity()));
//                        pdRecord.setNewQuantity(String.valueOf(detail.getInQuantity()));
//                        pdRecord.setRecordType(1);
//                        pdRecord.setCreateTime(sdf.format(new Date()));
//                        pdRecordMapper.insertPdRecord(pdRecord);
//
//                        //作业记录更新
//                        mydoJobDetail.setInQuantity(detail.getInQuantity());
//                        mydoJobDetailMapper.updateMydoJobDetail(mydoJobDetail);
//                    }
//
//                    // 返回数量为0时，进行删除操作
//                    if (detail.getInQuantity().intValue() == 0) {
//                        //盘点记录更新
//                        PdRecord pdRecord = new PdRecord();
//                        pdRecord.setPdGuid(UUID.randomUUID().toString());
//                        pdRecord.setStockId(mydoJobDetail.getStockId());
//                        pdRecord.setTrayCode(detail.getTrayCode());
//                        pdRecord.setProductId(mydoJobDetail.getProductId());
//                        pdRecord.setSupplier(mydoJobDetail.getSpec());
//                        pdRecord.setOldQuantity(String.valueOf(mydoJobDetail.getInQuantity()));
//                        pdRecord.setNewQuantity("0");
//                        pdRecord.setRecordType(3);
//                        pdRecord.setCreateTime(sdf.format(new Date()));
//                        pdRecordMapper.insertPdRecord(pdRecord);
//
//                        // 删除作业明细
//                        mydoJobDetailMapper.deleteMydoJobDetailByJobDetailId(detail.getJobDetailId());
//                    }
//                }
//            }
//        } catch (Exception e) {
//            return "盘点出现错误，错误信息为：" + e.getMessage();
//        }
//
//        return "操作成功！";
//    }

    @Autowired
    PdCommitRecordMapper pdCommitRecordMapper;

    @Override
    @Transactional
    public String pdStockSure(List<MydoJobDetail> detailList) {
        String msg = "";
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (MydoJobDetail detail : detailList) {
                //是否已填入数量
                if (detail.getInQuantity() == null || detail.getInQuantity() == new BigDecimal(BigInteger.ZERO)) {
                    return "请填写盘点后数量！";
                } else {
                    PdCommitRecord pdCommitRecord = new PdCommitRecord();
                    pdCommitRecord.setPdGuid(UUID.randomUUID().toString());
                    pdCommitRecord.setTrayCode(detail.getTrayCode());
                    pdCommitRecord.setStockId(detail.getStockId());
                    pdCommitRecord.setQuantity(String.valueOf(detail.getInQuantity()));
                    pdCommitRecordMapper.insertPdCommitRecord(pdCommitRecord);
                }
            }

            Map<String, String> params = new HashMap();
            params.put("v_tray_code", detailList.get(0).getTrayCode());
            params.put("v_result", null);
            rfMapper.pdmakeSure(params);
            String v_result = params.get("v_result");
            if (v_result.equals("1")) {
                msg = "无该条码任务，请检查！";
            } else if (v_result.equals("10")) {
                msg = "操作成功！";
            } else {
                msg = "未知的返回值，请联系管理员！";
            }
        } catch (Exception e) {
            return "盘点出现错误，错误信息为：" + e.getMessage();
        }

        return msg;
    }

    @Override
    public synchronized AjaxResult trayLIn(String trayCode) {
        String msg = "";
        Map<String, Object> params = new HashMap<String, Object>();

        params.put("trayCode", trayCode);
        params.put("inId", "101");
        params.put("result", 0);
        params.put("result_msg", "");

        mydoJobMapper.trayLIn(params);
        int result = (int) params.get("result");
        msg = (String) params.get("result_msg");
        if (result == 10) {
            return AjaxResult.success(msg);
        } else {
            return AjaxResult.error(msg);
        }

    }

    @Override
    public synchronized String trayIn(String trayCode, Integer quantity) {

        String msg = "";
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("v_type", "TPR");
        params.put("v_tray_code", trayCode);
        params.put("v_quantity", quantity);

        mydoJobMapper.trayIn(params);

//        int resultCode = (int) params.get("resultCode");
////        if (resultCode == 1) {
////            msg = "已有该托盘的入库作业！";
////        } else if (resultCode == 10) {
////            msg = "操作成功！";
////        } else {
////            msg = "未知的返回值，请检查！";
////        }
        msg = (String) params.get("v_resutmsg");
        return msg;
    }

    @Override
    public String trayOut(String exitId) {
        String msg = "";


        Map<String, Object> params = new HashMap<String, Object>();
        params.put("v_type", "TPC");
        params.put("v_exit", exitId);
        mydoJobMapper.trayOut(params);

        msg = (String) params.get("v_resutmsg");
        return msg;
    }

    @Override
    public synchronized AjaxResult trayLOut(String exitId, String quantity) {
        String msg = "";
        Map<String, Object> params = new HashMap<String, Object>();

        params.put("exitId", exitId);
        params.put("exitQuantity", quantity);
        params.put("warehouseId", "02");
        params.put("result", 0);
        params.put("result_msg", "");

        mydoJobMapper.trayLOut(params);
        int result = (int) params.get("result");
        msg = (String) params.get("result_msg");
        if (result == 10) {
            return AjaxResult.success(msg);
        } else {
            return AjaxResult.error(msg);
        }
    }

    @Override
    public AjaxResult selectTrayInfo(String trayCode) {
        //根据托盘码查询库存信息
        String taiCarType = "";
        List<BasStock> stockList = new ArrayList<>();

//        stockList = basStockMapper.selectBasStockByTrayCode(trayCode);
        MydoJob mydoJob = new MydoJob();
        mydoJob.setTrayCode(trayCode);
        List<MydoJob> mydoJobs = mydoJobMapper.selectMydoJobList(mydoJob);
        if (mydoJobs.size() > 0 && mydoJobs.get(0).getCarNumber() != null) {
            taiCarType = mydoJobs.get(0).getCarNumber();
        }
//        if (stockList.size() == 0) {
        //库存信息为空时候，查询作业明细内容
        List<MydoJobDetail> detailList = mydoJobDetailMapper.selectDeatilListByTray(trayCode);
        if (detailList.size() == 0) {
            return AjaxResult.error("托盘信息为空！");
        } else {
            for (MydoJobDetail detail : detailList) {

                BasStock stock = new BasStock();
                stock.setTrayCode(detail.getTrayCode());
                stock.setProductId(detail.getProductId());
                stock.setProductDetailId(detail.getProductDetailId());
                stock.setOutQuantity(detail.getOutQuantity());
                stock.setLineId(detail.getProductName());
                stock.setInClassName(detail.getSpec());
                stock.setTaskId(detail.getSlope());
                List<BasStock> basStocks = basStockMapper.selectBasStockByTrayCodeAndProductId(trayCode,detail.getProductId());
                if (basStocks.size() > 0) {
                    BigDecimal quantity = basStocks.get(0).getQuantity();
                    BigDecimal outQuantity = basStocks.get(0).getOutQuantity();
                    stock.setGiveNum(quantity.subtract(outQuantity));
                }
                stockList.add(stock);
            }
        }
//        }else {
//
//            for (BasStock basStock : stockList) {
//                BigDecimal quantity = basStock.getQuantity();
//                BigDecimal outQuantity = basStock.getOutQuantity();
//                basStock.setGiveNum(quantity.subtract(outQuantity));
//                basStock.setTaiCarType(taiCarType);
//            }
//        }
        Map<String, Object> map = new HashMap<>();
        map.put("data", stockList);
        map.put("carType", taiCarType);

        return AjaxResult.success("操作成功！", map);
    }

    @Override
    public synchronized String pickingIn(BasStock basStock) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("v_cargoSpaceId", basStock.getCargoSpaceId());
        params.put("v_trayCode", basStock.getTrayCode());
        params.put("v_productId", basStock.getProductId());
        params.put("v_in_quantity", basStock.getQuantity());
        params.put("v_result", "");
        params.put("v_resutmsg", "");
        mydoJobMapper.pickingIn(params);
        return (String) params.get("v_resutmsg");
    }

    @Override
    public String pickingOut(BasStock basStock) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("v_cargoSpaceId", basStock.getCargoSpaceId());
        params.put("v_trayCode", basStock.getTrayCode());
        params.put("v_result", "");
        params.put("v_resutmsg", "");
        mydoJobMapper.pickingOut(params);
        return (String) params.get("v_resutmsg");
    }

    @Override
    public List<MydoJobDetail> listByTrayCode(String trayCode) {
        return mydoJobDetailMapper.selectDeatilByTray(trayCode);
    }


    @Override
    public synchronized String pickingIn2(BasStock basStock) {
        Map<String, Object> params = new HashMap<String, Object>();

        params.put("v_trayCode", basStock.getTrayCode());

        params.put("v_result", "");
        params.put("v_resutmsg", "");
        mydoJobMapper.pickingIn2(params);
        return (String) params.get("v_resutmsg");
    }

    public synchronized AjaxResult inTemp(Map<String, String> params) {
        String trayCode = params.get("trayCode");
        String productId = params.get("productId");
        String quantity = params.get("quantity");
        if (StringUtils.isBlank(trayCode)) {
            return AjaxResult.error("托盘码为空");
        }
        if (StringUtils.isBlank(productId)) {
            return AjaxResult.error("物料码为空");
        }
        if (StringUtils.isBlank(quantity)) {
            return AjaxResult.error("数量为空");
        }
        if (!NumberUtils.isNumber(quantity)) {
            return AjaxResult.error("请输送数量");
        }
        //调用存储过程
        HashMap<String, Object> map = new HashMap<>();
        map.put("trayCode", trayCode);
        map.put("productId", productId);
        map.put("quantity", quantity);
        map.put("warehouseId", "02");
        map.put("result", 0);
        map.put("resultMsg", "未知");

        mydoJobMapper.callInTemp(map);

        int result = (int) map.get("result");
        String resultMsg = (String) map.get("resultMsg");

        if (result == 10) {
            return AjaxResult.success(resultMsg);
        }

        return AjaxResult.error(resultMsg);
    }

    public synchronized AjaxResult inTempFinish(Map<String, String> params) {
        String robotNo = params.get("robotNo");
        String workNo = params.get("workNo");
        if (StringUtils.isBlank(robotNo)) {
            return AjaxResult.error("机械手选择为空");
        }
        if (StringUtils.isBlank(workNo)) {
            return AjaxResult.error("工位选择为空");
        }

        //调用存储过程
        HashMap<String, Object> map = new HashMap<>();
        map.put("robotNo", robotNo);
        map.put("workNo", workNo);

        map.put("result", 0);
        map.put("resultMsg", "未知");

        mydoJobMapper.inTempFinish(map);

        int result = (int) map.get("result");
        String resultMsg = (String) map.get("resultMsg");

        if (result == 10) {
            return AjaxResult.success(resultMsg);
        }

        return AjaxResult.error(resultMsg);
    }

    public synchronized AjaxResult inTempSure(Map<String, String> params) {
        String trayCode = params.get("trayCode");
        String workNo = params.get("workNo");
        String jobType = params.get("jobType");
        if (StringUtils.isBlank(trayCode)) {
            return AjaxResult.error("托盘码为空");
        }
        if (StringUtils.isBlank(workNo)) {
            return AjaxResult.error("工位为空");
        }
        if (StringUtils.isBlank(jobType)) {
            return AjaxResult.error("作业类型为空");
        }

        //调用存储过程
        HashMap<String, Object> map = new HashMap<>();
        map.put("trayCode", trayCode);
        map.put("workNo", workNo);
        map.put("jobType", jobType);

        map.put("result", 0);
        map.put("resultMsg", "未知");

        mydoJobMapper.inTempSure(map);

        int result = (int) map.get("result");
        String resultMsg = (String) map.get("resultMsg");

        if (result == 10) {
            return AjaxResult.success(resultMsg);
        }

        return AjaxResult.error(resultMsg);
    }

    /**
     * 盘点入库
     *
     * @param trayCode
     * @return
     */
    @Override
    public String pdStockSureIn(String trayCode) {
        try {
            String msg = "";
            Map<String, String> params = new HashMap();
            params.put("v_tray_code", trayCode);
            params.put("v_result", null);
            rfMapper.pdStockSureIn(params);
            String v_result = params.get("v_result");
            if (v_result.equals("1")) {
                msg = "无该条码的盘点作业，盘点入库失败！";
            } else if (v_result.equals("10")) {
                msg = "操作成功！";
            } else if (v_result.equals("2")) {
                msg = "通过托盘编码找到多条作业 请检查！";
            } else {
                msg = "未知的返回值，请联系管理员！";
            }
            return msg;
        } catch (Exception e) {
            return "未知错误 请联系管理员";
        }
    }


    /**
     * ***
     *
     * @param
     * @return
     */
    @Override
    public AjaxResult changeSpace(String trayCode1, String trayCode2, String selectCargo) {
        Map<String, String> map = new HashMap();
        map.put("trayCode1", trayCode1);
        map.put("trayCode2", trayCode2);
        map.put("selectCargo", selectCargo);
        map.put("result", "");
        map.put("v_result_msg", "");
        rfMapper.changeSpacebyc(map);
        String v_result = map.get("v_result");
        String msg = map.get("v_result_msg");

        if ("操作成功！".equals(msg)) {
            return AjaxResult.success(msg);
        } else {
            return AjaxResult.error(msg);
        }
    }


    @Override
    public AjaxResult addKTPIn(String trayCode, String entrance) {
        Map<String, String> map = new HashMap();
        map.put("trayCode", trayCode);
        map.put("entrance", entrance);
        map.put("result", "");
        map.put("v_result_msg", "");
        rfMapper.addKTPIn(map);
        String v_result = map.get("v_result");
        String msg = map.get("v_result_msg");

        if ("操作成功！".equals(msg)) {
            return AjaxResult.success(msg);
        } else {
            return AjaxResult.error(msg);
        }
    }

    @Override
    public AjaxResult outSure(String trayCode, String jxtrayCode, String newTarSite) {
        Map<String, String> map = new HashMap();
        map.put("trayCode", trayCode);
        map.put("jxtrayCode", jxtrayCode);
        map.put("new_tarSite", newTarSite);
        map.put("result", "");
        map.put("result_msg", "");
        rfMapper.outSure412(map);
        String v_result = map.get("v_result");
        String msg = map.get("v_result_msg");

        if ("10".equals(v_result)) {
            return AjaxResult.success(msg);
        } else {
            return AjaxResult.error(msg);
        }
    }

    @Override
    public List<BasJxTaiChe> selectTray(String trayCode) {


        return rfMapper.selectTray(trayCode);
    }

    @Override
    public AjaxResult submitTaiChe(String trayCode, String entranceId) {

        Map<String, String> map = new HashMap();
        map.put("siteId", trayCode);
        map.put("trayType", entranceId);
        map.put("v_result", "");
        map.put("v_resultmsg", "");
        rfMapper.callEmptyTaiCar(map);
        String v_result = map.get("v_result");
        String msg = map.get("v_resultmsg");

        if ("10".equals(v_result)) {
            return AjaxResult.success(msg);
        } else {
            return AjaxResult.error(msg);
        }

    }

    @Override
    public AjaxResult ktpInStock(String trayCode, String theFloor, String thePlatoon) {
        Map<String, String> map = new HashMap();
        map.put("v_tray_code", trayCode);
        map.put("v_warehouseId", "01");
        map.put("v_the_platoon", thePlatoon);
        map.put("v_the_floor", theFloor);

        map.put("v_result", "");
        map.put("v_result_msg", "");
        rfMapper.ktpInStock(map);
        String v_result = map.get("v_result");
        String msg = map.get("v_result_msg");

        if ("10".equals(v_result)) {
            return AjaxResult.success(msg);
        } else {
            return AjaxResult.error(msg);
        }

    }

    @Override
    public AjaxResult storeGoods(String trayCode) {
        Map<String, String> map = new HashMap();
        map.put("trayCode", trayCode);
        map.put("v_result", "");
        map.put("v_resultmsg", "");
        rfMapper.storeGood(trayCode);
        String v_result = map.get("v_result");
        String msg = map.get("v_resultmsg");

        if ("10".equals(v_result)) {
            return AjaxResult.success(msg);
        } else {
            return AjaxResult.error(msg);
        }
    }

    @Override
    public void updateSerialNoByTaskId(String taskId, String serialNo) {
        rfMapper.updateSerialNoByTaskId(taskId, serialNo);
    }

}
